package cubelabs.nexus.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import cubelabs.nexus.entity.CounsellingEvent;
import cubelabs.nexus.entity.CounsellingEventStatus;

@Repository("counsellingEventDAO")
public class CounsellingEventDAO extends BaseDAO<CounsellingEvent, Long>{

	public List<CounsellingEvent> findByCounsellingAcceptorId(Long userId){
		/*CounsellingAcceptor counsellingAcceptor = new CounsellingAcceptor();
		User user = new User();
		user.setId(userId);
		counsellingAcceptor.setUser(user);*/
		List<CounsellingEvent> counsellingEvents = findByCriteria(Restrictions.and(Restrictions.eq("toUser.id", userId),Restrictions.eq("counsellingEventStatus", CounsellingEventStatus.Closed)));
		return counsellingEvents;
	}
	
	public List<CounsellingEvent> findByToUser(Long userId){
		/*CounsellingAcceptor counsellingAcceptor = new CounsellingAcceptor();
		User user = new User();
		user.setId(userId);
		counsellingAcceptor.setUser(user);*/
		List<CounsellingEvent> counsellingEvents = findByCriteria(
				 Restrictions.and(Restrictions.or(Restrictions.eq("frmUser.id", userId),Restrictions.eq("toUser.id", userId)),
						Restrictions.or(Restrictions.eq("counsellingEventStatus",CounsellingEventStatus.Active),
								Restrictions.eq("counsellingEventStatus",CounsellingEventStatus.InActive)),
								Restrictions.sqlRestriction("end > NOW()")));
		return counsellingEvents;
	}
	
	public List<CounsellingEvent> findByPendingToUser(Long userId){
		/*CounsellingAcceptor counsellingAcceptor = new CounsellingAcceptor();
		User user = new User();
		user.setId(userId);
		counsellingAcceptor.setUser(user);*/
		List<CounsellingEvent> counsellingEvents = findByCriteria(
				Restrictions.and(Restrictions.eq("toUser.id", userId),
						Restrictions.eq("counsellingEventStatus",CounsellingEventStatus.InActive)));
		return counsellingEvents;
	}
	
	public List<CounsellingEvent> findAboutToStartSession(Long timeDuration){
		List<CounsellingEvent> counsellingEvents = findByCriteria(
				Restrictions.and(Restrictions.eq("counsellingEventStatus",CounsellingEventStatus.Active),
						Restrictions.sqlRestriction("start <= DATE_ADD(NOW(), INTERVAL 1 MINUTE) and start > NOW()"),
						Restrictions.eq("isDeclined",false)));
		//Restrictions.sqlRestriction("where proposedStartTime > now() and proposedStartTime < DATE_ADD(now(), INTERVAL 2 hour)")
		return counsellingEvents;
		
	}
        
	@SuppressWarnings("unchecked")
	public Boolean isConflicted(Long userId,Date startTime,Date endTime) {
		Criteria crit = getSession().createCriteria(getPersistentClass());
		crit.add(
                        
                        Restrictions.and(
                            Restrictions.or(
                                Restrictions.and(
                                    Restrictions.ge("start", startTime),
                                    Restrictions.lt("end", startTime)),
                                
                                Restrictions.and(
                                    Restrictions.lt("start", endTime),
                                    Restrictions.gt("end", endTime))
                            ),
                        Restrictions.or(Restrictions.eq("frmUser.id", userId),
                                        Restrictions.eq("toUser.id", userId)
                        ),
                        Restrictions.eq("isActive", true)
                        )
                        );
                
      
		List<CounsellingEvent> counsellingEventList = (List<CounsellingEvent>)crit.list();
		if(counsellingEventList != null && counsellingEventList.size() > 0){
			return true;
		}
		return false;
	}

                
	@SuppressWarnings("unchecked")
	public List<CounsellingEvent> getCounsellingEvents(Long userId,Date startTime,Date endTime) {
		Criteria crit = getSession().createCriteria(getPersistentClass());
		crit.add(
                        
                        Restrictions.and(
                            Restrictions.or(
                                Restrictions.or(
                                    Restrictions.ge("start", startTime),
                                    Restrictions.lt("start", endTime)),
                                
                                Restrictions.or(
                                    Restrictions.lt("end", endTime),
                                    Restrictions.gt("end", startTime))
                            ),
                        Restrictions.or(Restrictions.eq("frmUser.id", userId),
                                        Restrictions.eq("toUser.id", userId)
                        ),
                        Restrictions.eq("isActive", true)
                        )
                        );
                
        	
		List<CounsellingEvent> counsellingEventList = (List<CounsellingEvent>)crit.list();
		if(counsellingEventList != null && counsellingEventList.size() > 0){
			return counsellingEventList;
		}
		return null;
	}

        
}
